<template>
  <div>
    <u-form
      :model="form"
      ref="uForm"
      :error-type="errorType"
      label-width="160rpx"
      label-align="center"
    >
      <u-form-item prop="author" label="用户"
        ><u-input v-model="form.author"
      /></u-form-item>
      <u-form-item prop="title" label="标题"
        ><u-input v-model="form.title"
      /></u-form-item>
      <u-form-item prop="authorName" label="作者"
        ><u-input v-model="form.authorName"
      /></u-form-item>
      <u-form-item prop="digest" label="概要"
        ><u-input v-model="form.digest"
      /></u-form-item>
      <u-form-item prop="topic" label="图片">
        <u-upload
          :action="action"
          :multiple="false"
          @on-uploaded="uploaded"
          :file-list="fileList"
        ></u-upload>
      </u-form-item>
      <u-form-item prop="formatText" label="内容"
        ><u-input
          type="textarea"
          v-model="form.formatText"
          height="200"
          maxlength="1000"
      /></u-form-item>
      <u-button type="success" @click="confim">发表文章</u-button>
    </u-form>
    <u-toast ref="uToast" />
  </div>
</template>
<script>
export default {
  data() {
    return {
      errorType: ["toast"],
      form: {
        title: "", // 标题
        authorName: "", // 作者
        author: "", // 用户
        createTime: "2021-05-21", // 创建时间
        digest: "", // 内容
        picUrl:
          "http://39.106.224.223:8088/ac/img/2021-05-21/fdbfha1864625657r.jpg", // 图片
        topic: "", // 头图
        formatText: "", // 文本格式？
        origionText: "fahugyewkul", // 来源 ？
        allowComment: 1, // 是否支持评论
        status: 1 // 什么的状态 ？
      },
      rules: {
        title: [
          {
            required: true,
            message: "标题不能为空",
            trigger: "blur"
          }
        ],
        authorName: [
          {
            required: true,
            message: "作者不能为空",
            trigger: "blur"
          }
        ],
        pulse: [
          {
            required: true,
            message: "脉搏不能为空",
            trigger: "blur"
          }
        ],
        author: [
          {
            required: true,
            message: "用户不能为空",
            trigger: "blur"
          }
        ],
        digest: [
          {
            required: true,
            message: "概要不能为空",
            trigger: "blur"
          }
        ],
        topic: [
          {
            required: true,
            message: "图片不能为空",
            trigger: "blur"
          }
        ],
        formatText: [
          {
            required: true,
            message: "内容不能为空",
            trigger: "blur"
          }
        ]
      },
      fileList: [],
      action: ""
    };
  },
  created() {
    console.log(Date.parse(new Date()));
    let dateTime = Date.parse(new Date());
    this.action = "http://39.106.224.223:8088/ac/upload/" + dateTime;
  },
  onReady() {
    this.$refs.uForm.setRules(this.rules);
  },
  methods: {
    uploaded(list, name) {
      // this.fileList = []
      this.form.topic = "http://39.106.224.223:8088" + list[0].response.data;
      //  this.fileList = [{url:"http://39.106.224.223:8088"+list[0].response.data}]
    },
    CurentTime() {
      var now = new Date();
      var year = now.getFullYear(); //年
      var month = now.getMonth() + 1; //月
      var day = now.getDate(); //日
      var clock = year + "-";
      if (month < 10) clock += "0";
      clock += month + "-";
      if (day < 10) clock += "0";
      clock += day + " ";
      return clock;
    },
    confim() {
      this.$refs["uForm"].validate(valid => {
        if (valid) {
          this.form.createTime = this.CurentTime();
          let that = this;
          let token = this.$store.state.token
          uni.request({
            url: "http://39.106.224.223:8088/cms/editior/essay/post",
            header: {
              "content-type": "application/json",
              Authorization: token
            },
            method: "POST",
            data: that.form,
            success: res => {
              if (res.data.code == 200) {
                this.$refs.uToast.show({
                  title: "发表成功",
                  type: "success"
                });
                uni.reLaunch({
                   url: './index'
                });
              } else {
                this.$refs.uToast.show({
                  title: "发表失败",
                  type: "error"
                });
              }
            },
            fail(er) {
              console.log(er);
            }
          });
        }
      });
    }
  }
};
</script>
<style lang="">
</style>